﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using DAL.Service.Impl;
using DAL.Service;
using DomainModel;

namespace BIZ
{
    public class UserCardOper : BaseOperation
    {
        IUserCardService userCardService = new UserCardServiceImpl();
        ILibraryBookIndentService indentService = new LibraryBookIndentServiceImpl();
        IUserCardCostService usercardcost = new UserCardCostServiceImpl();
        /// <summary>
        /// 保存会员卡信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public object save(object entity)
        {
            return userCardService.save(entity);
        }

        /// <summary>
        /// 删除会员卡
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int delete(int id)
        {
            return userCardService.delete(id);
        }

        /// <summary>
        /// 查询会员卡详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IList getUserCardbyid(int id)
        {
            return userCardService.getUserCardbyid(id);
        }

        /// <summary>
        /// 分页查询会员卡数据集合
        /// </summary>
        /// <param name="pageno"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IList getUserCard(string card, string in_s, string in_e, int pageno, int pagesize)
        {
            return userCardService.getUserCard(card, in_s, in_e, pageno, pagesize);
        }

        /// <summary>
        /// 验证会员卡是否已经存在
        /// </summary>
        /// <param name="card"></param>
        /// <returns></returns>
        public bool getCountByCard(string card, int id)
        {
            return userCardService.getCountByCard(card, id) > 0 ? true : false;
        }
        /// <summary>
        /// 获取充值卡信息
        /// </summary>
        /// <param name="card"></param>
        /// <returns></returns>
        public Hashtable getcardinfo(string card)
        {
            IList li = userCardService.getCard(card, 0);
            if (li.Count > 0)//充值卡是否存在
            {
                Hashtable tb = (Hashtable)li[0];
                return tb;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 充值卡充值验证
        /// </summary>
        /// <param name="uid">会员id</param>
        /// <param name="card">卡号</param>
        /// <param name="adminid">添加人ID</param>
        /// <param name="cmode">卡片类型</param>
        /// <param name="ckf">卡费</param>
        /// <param name="cyj">押金</param>
        /// <param name="cbz">备注</param>
        /// <returns></returns>
        public int getcard(int uid,string card,int adminid,string cmode,string ckf,string cyj,string cbz)
        { 
            IList li =userCardService.getCard(card, 0);
            if (li.Count > 0)//充值卡是否存在
            {
                Hashtable tb = (Hashtable)li[0];

                IList liCurrentCard = usercardcost.currentCardList(uid);//当前卡的已用次数列表
                DateTime startTime = DateTime.Now;
                if (liCurrentCard.Count > 0)//如果当前区间内卡还有使用次数则以当前卡的结束时间为新卡的起始时间，否则新卡立即生效
                {
                    foreach (Hashtable ctb in liCurrentCard)
                    {
                        if (int.Parse(ctb["c_times"].ToString()) == 0 || int.Parse(ctb["c_times"].ToString()) > int.Parse(ctb["usedTimes"].ToString()))
                        {
                            startTime = DateTime.Parse(ctb["c_endTime"].ToString());
                        }
                    }
                }

                UserCardCost ucc = new UserCardCost();//添加学员购买会员卡明细表记录
                ucc.c_uid = uid;
                ucc.c_ucid = int.Parse(tb["uc_id"].ToString());
                ucc.c_times = int.Parse(tb["uc_times"].ToString());
                ucc.c_user = adminid;
                ucc.c_endTime = startTime.AddMonths(int.Parse(tb["uc_useful"].ToString()));
                ucc.c_addTime = DateTime.Now;
                ucc.c_startTime = startTime;
                userCardService.save(ucc);

                userCardService.changestate(int.Parse(tb["uc_id"].ToString()), cmode, ckf, cyj, cbz,adminid.ToString());

                AdminOperationLog aol = new AdminOperationLog();//写操作记录
                aol.ol_user = adminid;
                aol.ol_table = "UserCard";
                aol.ol_resouceId = int.Parse(tb["uc_id"].ToString());
                aol.ol_time = DateTime.Now;
                aol.ol_action = "管理员ID：" + adminid.ToString() + "为用户ID：" + uid.ToString() + "充值，充值卡号：" + card;
                userCardService.save(aol);



                return 2;
            }
            else
            {
                return 1;//充值卡无效
            }
        }

        public IList getUserCard(string card, int type, int state, string in_s, string in_e,
          string sx_s, string sx_e,
          string dq_s, string dq_e,
          int ordercard, int ordertype, int orderstate, int orderdate, int orderuser,
          int pageno, int pagesize)
        {
            return userCardService.getUserCard(card, type, state, in_s, in_e,
             sx_s, sx_e,
             dq_s, dq_e,
             ordercard, ordertype, orderstate, orderdate, orderuser,
             pageno, pagesize);
        }
        /// <summary>
        /// 注销
        /// </summary>
        /// <param name="ucid">会员卡id</param>
        /// <param name="card">会员卡卡号</param>
        /// <param name="fy">费用</param>
        /// <param name="yj">押金</param>
        /// <param name="bz">备注</param>
        /// <param name="user">操作人</param>
        /// <returns>影响的行数 1成功 0失败</returns>
        public int updateUserCardState(int ucid,string card, string fy, string yj, string bz, int user)
        {
            int count = indentService.getIndentCountByUcid(ucid);
            if (count > 0)
                return -1;//该卡还有未归还的图书不能注销
            WriteOperationLog(user, "UserCard", ucid, "注销 卡号：" + card);

            //1.向退费表写记录
            UserCardTF tf = new UserCardTF()
            {
                uctf_ucid = ucid,
                uctf_fy = decimal.Parse(fy),
                uctf_yj = decimal.Parse(yj),
                uctf_bz = bz,
                uctf_user = user,
                uctf_addTime = DateTime.Now
            };
            save(tf);
            //2.修改会员卡状态
            return userCardService.updateState(ucid, (int)DAL.Enums.cardstate.已注销, user);
        }
    }
}
